package com.aabte.permissionframeworklearn.apacheshirodemo.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * @author Daniel
 */
@Slf4j
public class CredentialMatcher extends SimpleCredentialsMatcher {

    /**
     * 自定义密码校验
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        log.info("Start doCredentialsMatch.");

        // 获取用户输入的密码
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) (token);
        String password = new String(usernamePasswordToken.getPassword());

        // 获取数据库中的密码
        String passwordFromDb = (String) info.getCredentials();
        boolean result = this.equals(password, passwordFromDb);

        log.info("Start doCredentialsMatch. username: {}, result: {}", usernamePasswordToken.getUsername(), result);
        return result;
    }
}
